// Ray Bai
// Geometry Calculator with Functions
// Written in C++
// 5/25/2011

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

// Function prototypes
void printMenu();
void errorInvalidMenuOption(int);
bool validRadius(double);
double areaCircle(double);
void errorRadius();
bool validLengthAndWidth(double, double);
double areaRectangle(double, double);
void errorLengthAndWidth();
bool validBaseAndHeight(double, double);
double areaTriangle(double, double);
void errorBaseAndHeight();
void successfulTerminationOfProgramMessage();

int main()
{
    int selection;                          // For menu selection
    double radius;                          // For area of a circle
    double length, width;                   // For area of a rectangle
    double base, height;                    // For area of a triangle
    
    do {
       printMenu();                         // Display the menu                 
       cin >> selection;
       errorInvalidMenuOption(selection);   // If choice is invalid
                                            
       cout << fixed << showpoint << setprecision(4);
       
       // Respond to user's menu selection.
       switch (selection)
       {
        case 1:                      // Find area of a circle
             cout << endl;
             cout << "\t~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n";
             cout << "\t\tArea of a Circle\n\n";
             cout << "\tEnter radius: ";
             cin >> radius;
             
             if (validRadius(radius))   
             {
                cout << "\tArea = " << areaCircle(radius) << " square units";
                cout << "\n\n\t~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
             }   
             else
                errorRadius();
             break;
             
        case 2:                     // Find area of a rectangle
             cout << endl;
             cout << "\t~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n";
             cout << "\t\tArea of a Rectangle\n\n";
             cout << "\tEnter length: ";
             cin >> length;
             cout << "\tEnter width:  ";
             cin >> width;
             
             if (validLengthAndWidth(length, width))
             {
                cout << "\tArea = " << areaRectangle(length, width);
                cout << " square units";
                cout << "\n\n\t~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
             }
             else
                errorLengthAndWidth();
             break;
             
         case 3:                     // Find area of a triangle
             cout << endl;
             cout << "\t~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n";
             cout << "\t\tArea of a Triangle\n\n";
             cout << "\tEnter base:   ";
             cin >> base;
             cout << "\tEnter height: ";
             cin >> height;
              
             if (validBaseAndHeight(base, height))
             {
                cout << "\tArea = " << areaTriangle(base, height); 
                cout << " square units";
                cout << "\n\n\t~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";  
             }
             else
                errorBaseAndHeight();
             break;
              
         case 4:                     // Quit the program
              successfulTerminationOfProgramMessage();     
         }
        } while(selection != 4);
        
    system("pause");
    return 0;
}

void printMenu()                          // Print menu 
{
     cout << "\n\t\tGeometry Calculator\n\n";
     cout << "\t1. Calculate the Area of a Circle\n";
     cout << "\t2. Calculate the Area of a Rectangle\n";
     cout << "\t3. Calculate the Area of a Triangle\n";
     cout << "\t4. Quit\n\n";
     cout << "\tEnter your choice (1-4): ";
}

void errorInvalidMenuOption(int choice)  // Error message if choice invalid
{
     if (choice < 1 || choice > 4)
     {
          cout << endl; 
          cout << "\t**Error: Menu option must be 1, 2, 3, or 4.\n\n";
          cout << "\t~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
     }
}
 
bool validRadius(double rad)             // Validate that radius is valid
{
     bool inputstatus;
  
     if (rad > 0)
        inputstatus = true;
     else
        inputstatus = false;
     return inputstatus;
}

double areaCircle(double rad)            // Calculate area of circle
{
     const double PI = 3.14159;          // Set PI as constant
     return (PI * pow(rad, 2.0));   
}

void errorRadius()                       // Error message if radius invalid
{
     cout << endl;
     cout << "\t**Error: Radius cannot be negative.\n\n";
     cout << "\t~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
}

bool validLengthAndWidth(double l, double w)    // Validate that length and
                                                // width are valid
{
     bool inputstatus;
     
     if ((l >= 0) && (w >= 0))
        inputstatus = true;
     else
        inputstatus = false;
     return inputstatus;
}

double areaRectangle(double l, double w)       // Calculate area of rectangle
{
     return (l * w); 
}

void errorLengthAndWidth()                    // Error message if either length
                                              // or width is invalid 
{
     cout << endl;
     cout << "\t**Error: Length and width must be positive ";
     cout << "values.\n\n";
     cout << "\t~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
}

bool validBaseAndHeight(double b, double h)    // Validate that base and
                                               // height are valid
{
     bool inputstatus;
     
     if ((b >= 0) && (h >= 0))
        inputstatus = true;
     else
        inputstatus = false;
     return inputstatus;
}

double areaTriangle(double b, double h)        // Calculate area of triangle
{
    return (0.5 * b * h);        
}

void errorBaseAndHeight()                      // Error message is either base
                                               // or height is invalid
{
     cout << endl;
     cout << "\t**Error: Base and height must be positive ";
     cout << "values.\n\n";
     cout << "\t~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
}

void successfulTerminationOfProgramMessage()   // Message if you quit program
{
     cout << "\n\n\tThank you for using ";
     cout << "the Geometry Calculator.\n\n\n\n";
     cout << "You have now exited the program. ";
}  
